
let cacheName = 'workbox-merak-v2';
if ('serviceWorker' in navigator) {

    window.addEventListener('load',
        () => {
            let cachesKeys = caches.keys();

            navigator.serviceWorker.ready.then(registration => {
                console.log("serviceWoker 已经准好！");
                var ls = [];
                var timeStmipStorage = window.localStorage.getItem('timeStmip');

                if (timeStmipStorage != timeStmip) {//版本更新了
                    window.localStorage.setItem('timeStmip',timeStmip);
                    caches.open('workbox-merak-v2').then(cache => {
                        cache.keys().then(keys => { 
                            keys.map(item => {
                                let url = item.url;
                                let location = new URL(url);
                                let path = location.pathname + location.search
                                ls.push(path);
                                cache.delete(path);
                            })
                        })

                        setTimeout(() => {
                        console.log('重新注入 cacheStorge')
                            ls.map(item => {
                                cache.add(item);
                            })
                        }, 2000)
                    })
                }



            });
            //   registration.unregister();
            //  navigator.serviceWorker.register('/sw.js?v=1715398836526', { scope: '/' })

        })
}
